package com.jxb.demo.config;

import com.alibaba.fastjson.JSONObject;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.scripting.support.ResourceScriptSource;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.sound.midi.Soundbank;
import java.util.ArrayList;
import java.util.List;

/**
 * @desc redis的脚步服务
 */
@Component
public class RedisLuaService {

    @Resource
    private RedisTemplate redisTemplate;

    @Bean
    public DefaultRedisScript<Object> redisScript(){
        DefaultRedisScript<Object> redisScript = new DefaultRedisScript<>();
        redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("script/test.lua")));
        redisScript.setResultType(Object.class);
        return redisScript;
    }



    public void test(){
        DefaultRedisScript<Boolean> redisScript = new DefaultRedisScript<>();
        redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("script/test.lua")));
        redisScript.setResultType(Boolean.class);
        System.out.println(redisScript.getScriptAsString());
        List<String> keyList = new ArrayList<>();
        keyList.add("abc");
        Object obj = redisTemplate.execute(redisScript,keyList);
        System.out.println(obj);
    }
}
